package com.educate.educenter.controller;


import com.educate.commonutils.JwtUtils;
import com.educate.commonutils.ResultOne;
import com.educate.commonutils.ordervo.UcenterMemberOrder;
import com.educate.educenter.entity.UcenterMember;
import com.educate.educenter.entity.vo.RegisterVo;
import com.educate.educenter.mapper.UcenterMemberMapper;
import com.educate.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author educate.wht
 * @since 2022-08-25
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;
    //登录
    @PostMapping ("login")
    public ResultOne loginUser(@RequestBody UcenterMember ucenterMember){
        //调用service方法实现登录
        //返回token值 使用jwt生成
        String token=ucenterMemberService.login(ucenterMember);
        return ResultOne.ok().data("token",token);
    }
    //注册
    @PostMapping("register")
    public ResultOne registerUser(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return ResultOne.ok();

    }
    //根据token获取用户信息
    @GetMapping("getMemberInfo")
    public ResultOne getMemberInfo(HttpServletRequest request){
        //调用jwt工具的方法,根据request对象获取头信息,返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //根据数据库根据用户id获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        return ResultOne.ok().data("userInfo",member);
    }
    //根据用户id获取用户的信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
        UcenterMember member = ucenterMemberService.getById(id);
        //把member对象里面值赋值给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }
    //查询某一天的注册人数
    @GetMapping("countRegister/{day}")
    public ResultOne countRegister(@PathVariable String day){
        Integer count=ucenterMemberService.countRegisterDay(day);
        return ResultOne.ok().data("countRegister",count);
    }


}

